﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform.UC
{
    public partial class SimplifiedProcessItem : UserControl
    {
        public SimplifiedProcessItem()
        {
            InitializeComponent();

            cbEnabledIndicationError.Visible = false;
            cbEnabledIndicationError.Checked = true;
            cbEnabledRepeatability.Visible = false;
            cbEnabledRepeatability.Checked = true;

            gasCircuitLow.Event_EnabledChanged += GasCircuitLow_Event_EnabledChanged;
            gasCircuitHeight.Event_EnabledChanged += GasCircuitHeight_Event_EnabledChanged;
            gasCircuitMiddel.Event_EnabledChanged += GasCircuitMiddel_Event_EnabledChanged;

            cbGasCircuitLowIndicationError.CheckedChanged += CbGasCircuitLowIndicationError_CheckedChanged;
            cbGasCircuitHeightIndicationError.CheckedChanged += CbGasCircuitHeightIndicationError_CheckedChanged;
            cbGasCircuitMiddelIndicationError.CheckedChanged += CbGasCircuitMiddelIndicationError_CheckedChanged;

            tbFlowValue.Maximum = float.MaxValue;
            txtPurgingTimeMax.Maximum = float.MaxValue;
            txtPurgingConcentration.Maximum = float.MaxValue;
            tbtbDetectionErrorIndicationError.Maximum = float.MaxValue;
            tbDetectionErrorRepeatability.Maximum = float.MaxValue;
            tbAlarmSetValueAlarmError.Maximum = float.MaxValue;
            tbDetectionErrorAlarmError.Maximum = float.MaxValue;
        }

        private void CbGasCircuitHeightIndicationError_CheckedChanged(object sender, EventArgs e)
        {
            cbGasCircuitHeightRepeatability.Checked = cbGasCircuitHeightIndicationError.Checked;
        }

        private void CbGasCircuitLowIndicationError_CheckedChanged(object sender, EventArgs e)
        {
            cbGasCircuitLowRepeatability.Checked = cbGasCircuitLowIndicationError.Checked;
        }

        private void CbGasCircuitMiddelIndicationError_CheckedChanged(object sender, EventArgs e)
        {
            cbGasCircuitMiddelRepeatability.Checked = cbGasCircuitMiddelIndicationError.Checked;
        }

        private void GasCircuitMiddel_Event_EnabledChanged(object sender, EventArgs e)
        {
            cbGasCircuitMiddelIndicationError.Enabled = gasCircuitMiddel.Enabled;
            cbGasCircuitMiddelIndicationError.Checked = gasCircuitMiddel.Enabled;
            cbGasCircuitMiddelRepeatability.Enabled = gasCircuitMiddel.Enabled;
            cbGasCircuitMiddelRepeatability.Checked = gasCircuitMiddel.Enabled;
            cbGasCircuitMiddelResponseTime.Enabled = gasCircuitMiddel.Enabled;
            cbGasCircuitMiddelResponseTime.Checked = gasCircuitMiddel.Enabled;
        }

        private void GasCircuitHeight_Event_EnabledChanged(object sender, EventArgs e)
        {
            cbGasCircuitHeightIndicationError.Enabled = gasCircuitHeight.Enabled;
            cbGasCircuitHeightIndicationError.Checked = gasCircuitHeight.Enabled;
            cbGasCircuitHeightRepeatability.Enabled = gasCircuitHeight.Enabled;
            cbGasCircuitHeightRepeatability.Checked = gasCircuitHeight.Enabled;
            cbGasCircuitHeightResponseTime.Enabled = gasCircuitHeight.Enabled;
            cbGasCircuitHeightResponseTime.Checked = gasCircuitHeight.Enabled;
        }

        private void GasCircuitLow_Event_EnabledChanged(object sender, EventArgs e)
        {
            cbGasCircuitLowIndicationError.Enabled = gasCircuitLow.Enabled;
            cbGasCircuitLowIndicationError.Checked = gasCircuitLow.Enabled;
            cbGasCircuitLowRepeatability.Enabled = gasCircuitLow.Enabled;
            cbGasCircuitLowRepeatability.Checked = gasCircuitLow.Enabled;
            cbGasCircuitLowResponseTime.Enabled = gasCircuitLow.Enabled;
            cbGasCircuitLowResponseTime.Checked = gasCircuitLow.Enabled;
        }

        RelationsLMP.Model.SimplifiedProcess.SimplifiedProcessInfo simplifiedProcessInfo;
        public void Init(RelationsLMP.Model.SimplifiedProcess.SimplifiedProcessInfo SimplifiedProcessInfo,List<Model.DB.tb_ParaInfo> ParaInfoList)
        {
            simplifiedProcessInfo = SimplifiedProcessInfo;
            InitProcess();
        }

        public RelationsLMP.Model.SimplifiedProcess.SimplifiedProcessInfo SimplifiedProcessInfo
        {
            get
            {
                simplifiedProcessInfo.FlowValue = (float)tbFlowValue.Value;
                simplifiedProcessInfo.PurgingTimeMax = (int)txtPurgingTimeMax.Value;
                simplifiedProcessInfo.PurgingConcentration = (float)txtPurgingConcentration.Value;
                simplifiedProcessInfo.Description = tbDescription.Value;
                simplifiedProcessInfo.SoundAlert = cbSoundAlert.Checked;
                simplifiedProcessInfo.LightAlert = cbLightAlert.Checked;

                simplifiedProcessInfo.GasCircuitInfo.Clear();
                simplifiedProcessInfo.GasCircuitInfo.Add(gasCircuitLow.GasCircuitInfo);
                simplifiedProcessInfo.GasCircuitInfo.Add(gasCircuitMiddel.GasCircuitInfo);
                simplifiedProcessInfo.GasCircuitInfo.Add(gasCircuitHeight.GasCircuitInfo);

                simplifiedProcessInfo.IndicationErrorInfo.enabled = true;
                simplifiedProcessInfo.IndicationErrorInfo.DetectionError = (int)tbtbDetectionErrorIndicationError.Value;
                simplifiedProcessInfo.IndicationErrorInfo.gasCiruitTypeList.Clear();
                if (cbGasCircuitLowIndicationError.Checked)
                    simplifiedProcessInfo.IndicationErrorInfo.gasCiruitTypeList.Add(RelationsLMP.Model.SystemEnum.GasCiruitType.Low);
                if (cbGasCircuitMiddelIndicationError.Checked)
                    simplifiedProcessInfo.IndicationErrorInfo.gasCiruitTypeList.Add(RelationsLMP.Model.SystemEnum.GasCiruitType.Middel);
                if (cbGasCircuitHeightIndicationError.Checked)
                    simplifiedProcessInfo.IndicationErrorInfo.gasCiruitTypeList.Add(RelationsLMP.Model.SystemEnum.GasCiruitType.Height);                

                simplifiedProcessInfo.RepeatabilityInfo.enabled = true;
                simplifiedProcessInfo.RepeatabilityInfo.DetectionError = (float)tbDetectionErrorRepeatability.Value;
                if (cbGasCircuitLowRepeatability.Checked)
                    simplifiedProcessInfo.RepeatabilityInfo.gasCiruitType = RelationsLMP.Model.SystemEnum.GasCiruitType.Low;
                else if (cbGasCircuitMiddelRepeatability.Checked)
                    simplifiedProcessInfo.RepeatabilityInfo.gasCiruitType = RelationsLMP.Model.SystemEnum.GasCiruitType.Middel;
                else if (cbGasCircuitHeightRepeatability.Checked)
                    simplifiedProcessInfo.RepeatabilityInfo.gasCiruitType = RelationsLMP.Model.SystemEnum.GasCiruitType.Height;

                simplifiedProcessInfo.ResponseTimeInfo.enabled = cbEnabledResponseTime.Checked;
                simplifiedProcessInfo.ResponseTimeInfo.gasCiruitTypeList.Clear();
                if (cbGasCircuitLowResponseTime.Checked)
                    simplifiedProcessInfo.ResponseTimeInfo.gasCiruitTypeList.Add(RelationsLMP.Model.SystemEnum.GasCiruitType.Low);
                if (cbGasCircuitMiddelResponseTime.Checked)
                    simplifiedProcessInfo.ResponseTimeInfo.gasCiruitTypeList.Add(RelationsLMP.Model.SystemEnum.GasCiruitType.Middel);
                if (cbGasCircuitHeightResponseTime.Checked)
                    simplifiedProcessInfo.ResponseTimeInfo.gasCiruitTypeList.Add(RelationsLMP.Model.SystemEnum.GasCiruitType.Height);

                simplifiedProcessInfo.AlarmErrorInfo.enabled = cbEnabledAlarmError.Checked;
                simplifiedProcessInfo.AlarmErrorInfo.AlarmSetValue = (float)tbAlarmSetValueAlarmError.Value;
                simplifiedProcessInfo.AlarmErrorInfo.DetectionError = (float)tbDetectionErrorAlarmError.Value;

                return simplifiedProcessInfo;
            }
        }

        void InitProcess()
        {
            if (simplifiedProcessInfo == null) return;
            tbFlowValue.Value = simplifiedProcessInfo.FlowValue;
            txtPurgingTimeMax.Value = simplifiedProcessInfo.PurgingTimeMax;
            txtPurgingConcentration.Value = simplifiedProcessInfo.PurgingConcentration;
            tbDescription.Value = simplifiedProcessInfo.Description;
            cbSoundAlert.Checked = simplifiedProcessInfo.SoundAlert;
            cbLightAlert.Checked = simplifiedProcessInfo.LightAlert;
            Model.DB.tb_ParaInfo paraInfo = Static.ParaInfo.ParaInfoALL.Find(value => value.Id == simplifiedProcessInfo.GasName.Id);

            RelationsLMP.Model.SimplifiedProcess.GasCircuitInfo gasCircuitInfo;
            gasCircuitInfo = simplifiedProcessInfo.GasCircuitInfo.Find(value => value.type == RelationsLMP.Model.SystemEnum.GasCiruitType.Low);
            gasCircuitLow.ParaInfo = paraInfo;
            if (gasCircuitInfo != null)
            {
                gasCircuitLow.GasCircuitInfo = gasCircuitInfo;
                gasCircuitLow.Enabled = gasCircuitInfo.enabled;
            }
            else
                gasCircuitLow.Enabled = false;

            gasCircuitInfo = simplifiedProcessInfo.GasCircuitInfo.Find(value => value.type == RelationsLMP.Model.SystemEnum.GasCiruitType.Middel);
            gasCircuitMiddel.ParaInfo = paraInfo;
            if (gasCircuitInfo != null)
            {
                gasCircuitMiddel.GasCircuitInfo = gasCircuitInfo;
                gasCircuitMiddel.Enabled = gasCircuitInfo.enabled;
            }
            else
                gasCircuitMiddel.Enabled = false;

            gasCircuitInfo = simplifiedProcessInfo.GasCircuitInfo.Find(value => value.type == RelationsLMP.Model.SystemEnum.GasCiruitType.Height);
            gasCircuitHeight.ParaInfo = paraInfo;
            if (gasCircuitInfo != null)
            {
                gasCircuitHeight.GasCircuitInfo = gasCircuitInfo;
                gasCircuitHeight.Enabled = gasCircuitInfo.enabled;
            }
            else
                gasCircuitHeight.Enabled = false;

            cbEnabledIndicationError.Checked = simplifiedProcessInfo.IndicationErrorInfo.enabled;
            tbtbDetectionErrorIndicationError.Value = simplifiedProcessInfo.IndicationErrorInfo.DetectionError;
            foreach (RelationsLMP.Model.SystemEnum.GasCiruitType gasCiruitType in simplifiedProcessInfo.IndicationErrorInfo.gasCiruitTypeList)
            {
                switch (gasCiruitType)
                {
                    case RelationsLMP.Model.SystemEnum.GasCiruitType.Height:
                        cbGasCircuitHeightIndicationError.Checked = true;
                        break;
                    case RelationsLMP.Model.SystemEnum.GasCiruitType.Low:
                        cbGasCircuitLowIndicationError.Checked = true;
                        break;
                    case RelationsLMP.Model.SystemEnum.GasCiruitType.Middel:
                        cbGasCircuitMiddelIndicationError.Checked = true;
                        break;
                }
            }

            cbEnabledRepeatability.Checked = simplifiedProcessInfo.RepeatabilityInfo.enabled;
            tbDetectionErrorRepeatability.Value = simplifiedProcessInfo.RepeatabilityInfo.DetectionError;
            switch (simplifiedProcessInfo.RepeatabilityInfo.gasCiruitType)
            {
                case RelationsLMP.Model.SystemEnum.GasCiruitType.Height:
                    cbGasCircuitHeightRepeatability.Checked = true;
                    break;
                case RelationsLMP.Model.SystemEnum.GasCiruitType.Low:
                    cbGasCircuitLowRepeatability.Checked = true;
                    break;
                case RelationsLMP.Model.SystemEnum.GasCiruitType.Middel:
                    cbGasCircuitMiddelRepeatability.Checked = true;
                    break;
            }

            cbEnabledResponseTime.Checked = simplifiedProcessInfo.ResponseTimeInfo.enabled;
            foreach (RelationsLMP.Model.SystemEnum.GasCiruitType gasCiruitType in simplifiedProcessInfo.ResponseTimeInfo.gasCiruitTypeList)
            {
                switch (gasCiruitType)
                {
                    case RelationsLMP.Model.SystemEnum.GasCiruitType.Height:
                        cbGasCircuitHeightResponseTime.Checked = true;
                        break;
                    case RelationsLMP.Model.SystemEnum.GasCiruitType.Low:
                        cbGasCircuitLowResponseTime.Checked = true;
                        break;
                    case RelationsLMP.Model.SystemEnum.GasCiruitType.Middel:
                        cbGasCircuitMiddelResponseTime.Checked = true;
                        break;
                }
            }

            cbEnabledAlarmError.Checked = simplifiedProcessInfo.AlarmErrorInfo.enabled;
            tbDetectionErrorAlarmError.Value = simplifiedProcessInfo.AlarmErrorInfo.DetectionError;
            tbAlarmSetValueAlarmError.Value = simplifiedProcessInfo.AlarmErrorInfo.AlarmSetValue;
        }
    }
}
